<?php


namespace app\controllers;

use app\exception\ErrorCode;
use app\exception\OaException;
use app\models\UserDto;
use app\service\ApplyService;

use app\models\request\ReqId;


class CancelController extends BaseJsonController
{

    /**
     * @param $req ReqId
     * @param $userInfo UserDto
     * @return \app\models\response\RespLogin|mixed
     * @throws \app\exception\OaException
     */
    public function doService($req,$userInfo)
    {

        $service  = new ApplyService();
        $entity = $service->findByIdAndUserId($req->getId(),$userInfo->getUserId());
        if ($entity->getStatus() == 5) {
            throw new OaException(ErrorCode::cancel_already);
        }
        if ($entity->getStatus() == 6) {
            throw new OaException(ErrorCode::cancel_ok);
        }
        if ($entity->getStatus() == 3) {
            throw new OaException(ErrorCode::ACT_FINISHED);
        }

        if ($entity->getStatus() == 2) {
            throw new OaException(ErrorCode::ACT_STATUS_ERR);
        }

        if ($entity->getStatus() == 0) {
            $entity->setStatus(6);
        } else {
            $entity->setStatus(5);
        }

        $entity->save();
        return true;

    }

    protected function injectionReq()
    {
        return new ReqId();
    }

    public function needLogin(){
        return true;
    }
}